package com.study.selection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.selection.model.College;
import com.study.selection.model.Course;
import com.study.selection.model.Teacher;
import com.study.selection.model.Teachering;
import com.study.selection.service.CollegeService;
import com.study.selection.service.CourseService;
import com.study.selection.service.TeacherService;
import com.study.selection.service.TeacheringService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/teachering")
public class TeacheringController {
    // 注入Service
    @Autowired
    private TeacheringService teacheringService;
    @Autowired
    private CourseService courseService;
    // 查询所有,绑定数据。
    @RequestMapping("/listAll")
    public String doList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<Teachering> list = teacheringService.findAll();
        PageInfo<Teachering> info = new PageInfo<>(list,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/admin/approval.jsp";
    }
    // 根据关键字查询,绑定数据。
    @RequestMapping("/search")
    public String doSearch(@RequestParam(value = "page",defaultValue = "1")Integer page,String key,Model model){
        PageHelper.startPage(page,5);
        List<Teachering> list = teacheringService.findByKey(key);
        PageInfo<Teachering> info = new PageInfo<>(list,3);
        model.addAttribute("info",info);
        model.addAttribute("key",key);
        return "/view/admin/approval.jsp";
    }
//调课审批，拒绝调课请求
    @RequestMapping("/refuse")
    public String doRefuse(int teacheringId){
        boolean result=teacheringService.deleteTeachering(teacheringId);
        return "redirect:listAll";
    }
    //调课审批，同意调课请求
    @RequestMapping("/agree")
    public String doAgree(int teacheringId, Model model){
        Teachering teachering=teacheringService.getByTeacheringId(teacheringId);
        Course course=courseService.getByCourseId(teachering.getCourseId());
        course.setClassroom(teachering.getClassroom());
        course.setCourseTime(teachering.getCourseTime());
        boolean result1=courseService.updateCourse(course);
        boolean result2=teacheringService.deleteTeachering(teacheringId);
        if (result1&&result2){
            model.addAttribute("message","审批成功");
            return "redirect:listAll";
        }else {
            model.addAttribute("message","审批失败");
            return "redirect:listAll";
        }
    }
    @RequestMapping("/deleteItems")
    public String deleteItems(String[] chkIds) {
        for (String string : chkIds) {
            Teachering teachering=teacheringService.getByTeacheringId(Integer.parseInt(string));
            Course course=courseService.getByCourseId(teachering.getCourseId());
            course.setClassroom(teachering.getClassroom());
            course.setCourseTime(teachering.getCourseTime());
            courseService.updateCourse(course);
            teacheringService.deleteTeachering(Integer.parseInt(string));
        }
        return "redirect:listAll";
    }
}



